package org.brisling.service.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.brisling.domain.common.ListDomain;
import org.brisling.domain.sysman.inf.BizBeanDataModeler;
import org.brisling.domain.sysman.inf.UplinkDataModeler;
import org.brisling.repository.inf.ListByUpLinkRepository;
import org.brisling.util.LongUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import com.fasterxml.jackson.databind.ObjectMapper;

public class BaseUplinkService{

	//定义起始页是否为1
	private Boolean pageBasedOne=false;
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<? extends UplinkDataModeler> getAll(ListByUpLinkRepository repo){
		
		return repo.findAll(new Sort(new String[] { "sortedid" }));	
			
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ListDomain<? extends UplinkDataModeler> getPagedListByUplinkid(String uplinkid,String page,String pageSize,
			ListByUpLinkRepository repo){
		
		Integer _uplinkid = LongUtil.getIntValue(uplinkid, null);
		Integer _page = LongUtil.getIntValue(page, 0);
		Integer _pageSize = LongUtil.getIntValue(pageSize, 10);
		
		
		ListDomain<UplinkDataModeler> listDomain = new ListDomain<UplinkDataModeler>();
		//dept为空，返回空值对象
		if(_uplinkid==null){
			listDomain.setTotal(0l);
			listDomain.setRows(new ArrayList<UplinkDataModeler>());
			return listDomain;
		}
	
		if(!pageBasedOne)
			_page -= 1;
		
		PageRequest _pageRequest = 
				new PageRequest(_page,_pageSize,new Sort(new String[] { "sortedid" }));
		
		Long _total = repo.countByUplinkid(new Long(_uplinkid));
		List<UplinkDataModeler> _usrList = repo.findByUplinkid(new Long(_uplinkid), _pageRequest);		
		
		listDomain.setTotal(_total);
		listDomain.setRows(_usrList);
		
		return listDomain;
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T extends BizBeanDataModeler>T save(ListByUpLinkRepository repo,T t){
		return (T) repo.save(t);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String del(ListByUpLinkRepository repo,Long id){
		repo.delete(id);
		return "success";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String del(ListByUpLinkRepository repo,String id){
		Long _id = LongUtil.getLongValue(id, null);
		if(_id!=null){
			repo.delete(_id);
			return "200";
		}else{
			return "id must not be null !";
		}
		
	}
	
	@SuppressWarnings("rawtypes")
	public <T extends UplinkDataModeler> T  bodyToObj (String body,Class<T> clazz){
		ObjectMapper mapper = new ObjectMapper();
		
		try {
			T t =  mapper.readValue(body, clazz);
			return t;
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<? extends BizBeanDataModeler> getList(String tid,
			ListByUpLinkRepository _repo) {
		Long _tid = LongUtil.getLongValue(tid, null);
		
		if(_tid==null)
			return new ArrayList<BizBeanDataModeler>();
		
		List<BizBeanDataModeler> ite = _repo.findByUplinkid(_tid, new Sort(new String[] { "sortedid" }));
		
		return ite;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T extends UplinkDataModeler>T getOne(String tid,
			ListByUpLinkRepository _repo) {
		
		Long _tid = LongUtil.getLongValue(tid, null);
		T t = null;
		if(_tid!=null)
			t= (T) _repo.findOne(_tid);
		
		return t;
	}
	
	
}
